package com.zf.admin.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zf.admin.pojo.Houselist;
import com.zf.admin.service.HouselistService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
//import cn.itcast.utils.Page;


@Controller
public class HoustlistController {
    @Autowired
    private HouselistService houselistService;


    @RequestMapping("/ahouselist")
    public String ahouselist(Model model, @RequestParam(required = false, defaultValue = "1") Integer page,
                             @RequestParam(required = false, defaultValue = "2") Integer pageSize) {

        PageHelper.startPage(page, pageSize);
        List<Houselist> houselist = houselistService.selectAll();
        PageInfo<Houselist> p = new PageInfo<Houselist>(houselist);


        model.addAttribute("p", p);
        model.addAttribute("houselist", houselist);
        model.addAttribute("mainPage", "ahouselist.jsp");
        return "admin/main1";
    }

    @RequestMapping("/addhouse")
    public String addhouse(Model model, Houselist houselist) {

        String houseid = houselist.getHouseid();
        Houselist houselist1 = houselistService.findhouseid(houseid);
        if (houselist1 != null) {
            model.addAttribute("error", "该房屋id已存在");
            model.addAttribute("houselist", houselist);
            model.addAttribute("mainPage", "addhouse.jsp");
            return "admin/main1";
        } else {
            model.addAttribute("error", "添加成功");
            if (houselist.getImg().equals("") || houselist.getImg() == null) {
                houselist.setImg("https://pic1.ajkimg.com/display/xinfang/4bad6425dd0a65bdf4788c0a0036727f/190x130n.jpg");
            }
            houselistService.inserthouse(houselist);
            model.addAttribute("houselist", houselist);
            model.addAttribute("mainPage", "addhouse.jsp");
            return "admin/main1";
        }
    }

    @RequestMapping("/toaddhouse")
    public String toaddhoust(Model model) {
        model.addAttribute("mainPage", "addhouse.jsp");

        return "admin/main1";
    }

    @RequestMapping("/deletehouse")
    public String deletehouse(Integer id) {
        houselistService.deletehouse(id);


        return "redirect:ahouselist";
    }

    @RequestMapping("/toahouselist")
    public String toahouselist() {


        return "ahouselist";
    }

    @RequestMapping("/findid")
    public String findid(Integer id, Model model) {
        Houselist list = houselistService.findid(id);
        model.addAttribute("houselist", list);
        model.addAttribute("mainPage", "changehouse.jsp");
        return "admin/main1";
    }

    @RequestMapping("/findhouseidupdate")
    public String findhouseidupdate(Houselist houselist, Model model) {
        if (houselist.getImg().equals("") || houselist.getImg() == null) {
            houselist.setImg("https://pic1.ajkimg.com/display/xinfang/4bad6425dd0a65bdf4788c0a0036727f/190x130n.jpg");
        }
        Houselist list = houselistService.findhouseidupdate(houselist);
        if (list != null) {
            model.addAttribute("houselist", houselist);
            model.addAttribute("mainPage", "changehouse.jsp");
            model.addAttribute("error", "该房屋id已存在");
            return "admin/main1";
        } else {
            houselistService.updatehouse(houselist);
            model.addAttribute("houselist", houselist);
            model.addAttribute("mainPage", "changehouse.jsp");
            model.addAttribute("error", "更新成功");
            return "admin/main1";
        }
    }

}
